/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kota207;

/**
 *
 * @author Abahe
 */
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Abahe
 */
public class PlayGround extends GameCanvas implements Runnable, PlayerListener {

    private AyokLari midlet;
    private ServerForm server;
    private ClientForm client;
    //---------------------------------------------------------//
    //test speed
    private int iTestSpeed;
    //---------------------------------------------------------//
    //xy
    private int xBoard;
    private int yBoard;
    //---------------------------------------------------------//
    //image
    private Image bg;
    //---------------------------------------------------------//
    //sprite
    //private Sprite spPuatoMine;
    private Sprite spCursor;
    private Sprite spPuato1;
    private Sprite spPuato2;
    private Sprite spPuato3;
    private Sprite spPuato4;
    private LayerManager lmgr;
    //---------------------------------------------------------//
    //path
    private String pathPuato1;
    private String pathPuato2;
    private String pathPuato3;
    private String pathPuato4;
    //---------------------------------------------------------//
    //size
    private int boardWidth;
    private int boardHeight;
    //---------------------------------------------------------//
    //boolean
    private boolean running = false;
    private boolean isStart = false;
    //---------------------------------------------------------//
    //thread
    private long time = 0;
    private int speedGame = 250;
    //---------------------------------------------------------//
    //status
    private int status = 0;
    private int statusArah = 0;
    private String statusKey = "";
    private String lastStatus1 = "0";
    private String lastStatus2 = "0";
    private String lastStatus3 = "0";
    private String lastStatus4 = "0";
    //---------------------------------------------------------//
    //for sound
    private long delay;
    private Player sound01;
    private Player sound02;

    public PlayGround(AyokLari midlet, int status, ServerForm server, ClientForm client) {
        super(false);
        setFullScreenMode(true);
        this.midlet = midlet;
        this.status = status;
        this.server = server;
        this.client = client;

        xBoard = 10;
        yBoard = 10;

        pathPuato1 = "/puato_green_50x50.png";
        pathPuato2 = "/puato_pink_50x50.png";
        pathPuato3 = "/puato_brown_50x50.png";
        pathPuato4 = "/puato_blue_50x50.png";

        try {
            spCursor = new Sprite(Image.createImage("/cursors.png"), 50, 50);
            spCursor.setPosition(0, 112);
            spPuato1 = new Sprite(Image.createImage(pathPuato1), 50, 50);
            spPuato1.setPosition(0, 162);
            spPuato2 = new Sprite(Image.createImage(pathPuato2), 50, 50);
            spPuato2.setPosition(0, 162);
            spPuato3 = new Sprite(Image.createImage(pathPuato3), 50, 50);
            spPuato3.setPosition(0, 162);
            spPuato4 = new Sprite(Image.createImage(pathPuato4), 50, 50);
            spPuato4.setPosition(0, 162);

            bg = Image.createImage("/bg.png");
        } catch (IOException ex) {
        }

        /*
        if (status == 1) { //server
        spPuatoMine = new Sprite(spPuato1);
        spPuatoMine.setPosition(0, 100);
        spPuato1.setVisible(false);
        } else if (status == 2) { //client
        spPuatoMine = new Sprite(spPuato2);
        spPuatoMine.setPosition(0, 100);
        spPuato2.setVisible(false);
        } else if (status == 3) { //client
        spPuatoMine = new Sprite(spPuato3);
        spPuatoMine.setPosition(0, 100);
        spPuato3.setVisible(false);
        } else if (status == 4) { //client
        spPuatoMine = new Sprite(spPuato4);
        spPuatoMine.setPosition(0, 100);
        spPuato1.setVisible(false);
        }
         *
         */

        lmgr = new LayerManager();
        if (status == 1) {
            lmgr.append(spCursor);
            lmgr.append(spPuato1);
            lmgr.append(spPuato2);
            lmgr.append(spPuato3);
            lmgr.append(spPuato4);
        } else if (status == 2) {
            lmgr.append(spCursor);
            lmgr.append(spPuato2);
            lmgr.append(spPuato3);
            lmgr.append(spPuato4);
            lmgr.append(spPuato1);
        } else if (status == 3) {
            lmgr.append(spCursor);
            lmgr.append(spPuato3);
            lmgr.append(spPuato4);
            lmgr.append(spPuato1);
            lmgr.append(spPuato2);
        } else if (status == 4) {
            lmgr.append(spCursor);
            lmgr.append(spPuato4);
            lmgr.append(spPuato1);
            lmgr.append(spPuato2);
            lmgr.append(spPuato3);
        }

        try {
            sound01 = Manager.createPlayer(getClass().getResourceAsStream("/menu.mid"), "audio/midi");
            sound01.realize();
            sound01.prefetch();
            sound01.addPlayerListener(this);
            sound01.setLoopCount(-1);
            sound01.start();
            /*
            if (midlet.SOUND == 1) {
            sound01.start();
            } else {
            }
             *
             */
        } catch (Exception e) {
        }

        try {
            sound02 = Manager.createPlayer(getClass().getResourceAsStream("/game.mid"), "audio/midi");
            sound02.realize();
            sound02.prefetch();
            sound02.addPlayerListener(this);
            sound02.setLoopCount(-1);
            /*
            if (midlet.SOUND == 1) {
            sound01.start();
            } else {
            }
             *
             */
        } catch (Exception e) {
        }

    }

    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void setupAwal() {
        String stateGame = "";
        if (AyokLari.JUM_PLAYER == 1) {
            stateGame = "10000000000000000000000000000";
            server.broadcastToClients(String.valueOf(stateGame));
        } else if (AyokLari.JUM_PLAYER == 2) {
            stateGame = "10000002000000000000000000000";
            server.broadcastToClients(String.valueOf(stateGame));
        } else if (AyokLari.JUM_PLAYER == 3) {
            stateGame = "10000002000000300000000000000";
            server.broadcastToClients(String.valueOf(stateGame));
        } else if (AyokLari.JUM_PLAYER == 4) {
            stateGame = "10000002000000300000040000000";
            server.broadcastToClients(String.valueOf(stateGame));
        }
        AyokLari.STATE_GAME = stateGame;
    }

    public void run() {
        Graphics g = getGraphics();
        //with
        while (running) {
            time = System.currentTimeMillis();
            if (AyokLari.STATE_GAME.substring(28).equals("1") && isStart == false) {
                //System.out.println(AyokLari.STATE_GAME.substring(28));
                isStart = true;
                playGameMusic();
            }
            if (AyokLari.STATE_GAME.equals("start")) {
                if (status == 1) {
                    //System.out.println("jum : " + AyokLari.JUM_PLAYER);
                    //server.broadcastToClients(String.valueOf(AyokLari.JUM_PLAYER));
                }
                //System.out.println("cek : " + AyokLari.STATE_GAME + " yo " + AyokLari.STATE_GAME.length());

                /*
                if (AyokLari.STATE_GAME.length() >= 27) {
                isStart = true;
                }
                 *
                 */
            }
            if (isStart) {
                iTestSpeed++;
            }
            drawDisplay(g);
            time = speedGame - (System.currentTimeMillis() - time);
            try {
                Thread.sleep((time < 0 ? 0 : time));
            } catch (InterruptedException ie) {
            }
        }
    }

    private void drawDisplay(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(0x0000ff);
        //g.drawString(status, 20, 10, Graphics.TOP | Graphics.LEFT);
        /*
        if (status == 1) {
        g.drawString("Player : 1", 5, 5, Graphics.TOP | Graphics.LEFT);

        } else {
        g.drawString("Player : " + status, 5, 5, Graphics.TOP | Graphics.LEFT);
        }
         * 
         */

        g.drawImage(bg, 0, 0, Graphics.TOP | Graphics.LEFT);
        g.drawString("Player : " + status, 5, 5, Graphics.TOP | Graphics.LEFT);

        //g.drawString(AyokLari.STATE_GAME, 5, 20, Graphics.TOP | Graphics.LEFT);

        /*
        if(status==1){
        server.broadcastToClients(AyokLari.STATE_GAME);
        }else{
        client.broadcastToAll(AyokLari.STATE_GAME);
        }
         *
         */
        //g.drawString(String.valueOf(iTestSpeed), 50, 100, Graphics.TOP | Graphics.LEFT);
        //g.drawString(statusKey, 50, 100, Graphics.TOP | Graphics.LEFT);
        //g.drawImage(imgPuato, 70, 20, Graphics.TOP | Graphics.LEFT);
        lmgr.paint(g, 0, 0);
        spCursor.nextFrame();
        spPuato1.nextFrame();
        spPuato2.nextFrame();
        spPuato3.nextFrame();
        spPuato4.nextFrame();
        if (isStart) {
            runPuato();
        }

        flushGraphics();
    }

    private void runPuato() {
        //1000000240000034000000000000
        
        //System.out.println("cikan cik " + AyokLari.STATE_GAME);
        //System.out.println("cikan cik " + AyokLari.STATE_GAME.substring(8, 9));

        //System.out.println("last " + lastStatus);
        //puato2
        /* buat ke atas
        if (AyokLari.STATE_GAME.substring(15, 16).equals("1")) {
            if (spPuato2.getX() > 1) {
                if (lastStatus2.equals("4")) { //right
                    //System.out.println("mundur dulu");
                    spPuato2.setTransform(Sprite.TRANS_MIRROR);
                    spPuato2.move(50, 0);
                } else {
                    spPuato2.setTransform(Sprite.TRANS_MIRROR);
                    spPuato2.move(-10, 0);
                }
                //lastStatus2 = "1";
            }
        }
         * 
         */

        if (AyokLari.STATE_GAME.substring(8, 9).equals("3")) {
            if (spPuato2.getX() > 1) {
                if (lastStatus2.equals("4")) {
                    //System.out.println("mundur dulu");
                    spPuato2.setTransform(Sprite.TRANS_MIRROR);
                    spPuato2.move(50, 0);
                } else {
                    spPuato2.setTransform(Sprite.TRANS_MIRROR);
                    spPuato2.move(-10, 0);
                }
                lastStatus2 = "3";
            }
        }
        if (AyokLari.STATE_GAME.substring(8, 9).equals("4")) {
            if (spPuato2.getX() < (getWidth() - 50)) {
                if (lastStatus2.equals("3")) {
                    //System.out.println("mundur duya");
                    spPuato2.setTransform(Sprite.TRANS_NONE);
                    spPuato2.move(-50, 0);
                } else {
                    spPuato2.setTransform(Sprite.TRANS_NONE);
                    spPuato2.move(10, 0);
                }
                lastStatus2 = "4";
            }
        }
        
        //puato3
        if (AyokLari.STATE_GAME.substring(15, 16).equals("3")) {
            if (spPuato3.getX() > 1) {
                if (lastStatus3.equals("4")) {
                    //System.out.println("mundur dulu");
                    spPuato3.setTransform(Sprite.TRANS_MIRROR);
                    spPuato3.move(50, 0);
                } else {
                    spPuato3.setTransform(Sprite.TRANS_MIRROR);
                    spPuato3.move(-10, 0);
                }
                lastStatus3 = "3";
            }
        }
        if (AyokLari.STATE_GAME.substring(15, 16).equals("4")) {
            if (spPuato3.getX() < (getWidth() - 50)) {
                if (lastStatus3.equals("3")) {
                    //System.out.println("mundur duya");
                    spPuato3.setTransform(Sprite.TRANS_NONE);
                    spPuato3.move(-50, 0);
                } else {
                    spPuato3.setTransform(Sprite.TRANS_NONE);
                    spPuato3.move(10, 0);
                }
                lastStatus3 = "4";
            }
        }
        //puato4
        if (AyokLari.STATE_GAME.substring(22, 23).equals("3")) {
            if (spPuato4.getX() > 1) {
                if (lastStatus4.equals("4")) {
                    //System.out.println("mundur dulu");
                    spPuato4.setTransform(Sprite.TRANS_MIRROR);
                    spPuato4.move(50, 0);
                } else {
                    spPuato4.setTransform(Sprite.TRANS_MIRROR);
                    spPuato4.move(-10, 0);
                }
                lastStatus4 = "3";
            }
        }
        if (AyokLari.STATE_GAME.substring(22, 23).equals("4")) {
            if (spPuato4.getX() < (getWidth() - 50)) {
                if (lastStatus4.equals("3")) {
                    //System.out.println("mundur duya");
                    spPuato4.setTransform(Sprite.TRANS_NONE);
                    spPuato4.move(-50, 0);
                } else {
                    spPuato4.setTransform(Sprite.TRANS_NONE);
                    spPuato4.move(10, 0);
                }
                lastStatus4 = "4";
            }
        }
        //puato1
        if (AyokLari.STATE_GAME.substring(1, 2).equals("3")) {
            if (spPuato1.getX() > 1) {
                if (lastStatus1.equals("4")) {
                    //System.out.println("mundur dulu");
                    spPuato1.setTransform(Sprite.TRANS_MIRROR);
                    spPuato1.move(50, 0);
                } else {
                    spPuato1.setTransform(Sprite.TRANS_MIRROR);
                    spPuato1.move(-10, 0);
                }
                lastStatus1 = "3";
            }
        }
        if (AyokLari.STATE_GAME.substring(1, 2).equals("4")) {
            if (spPuato1.getX() < (getWidth() - 50)) {
                if (lastStatus1.equals("3")) {
                    //System.out.println("mundur duya");
                    spPuato1.setTransform(Sprite.TRANS_NONE);
                    spPuato1.move(-50, 0);
                } else {
                    spPuato1.setTransform(Sprite.TRANS_NONE);
                    spPuato1.move(10, 0);
                }
                lastStatus1 = "4";
            }
        }


        if (status == 1) {
            spCursor.setPosition(spPuato1.getX(), spCursor.getY());
        } else if (status == 2) {
            spCursor.setPosition(spPuato2.getX(), spCursor.getY());
        } else if (status == 3) {
            spCursor.setPosition(spPuato3.getX(), spCursor.getY());
        } else if (status == 4) {
            spCursor.setPosition(spPuato4.getX(), spCursor.getY());
        }

    }
    

    protected void keyPressed(int keyCode) {
        super.keyPressed(keyCode);
        System.out.println(keyCode);

        statusKey = String.valueOf(keyCode);
        if (keyCode == -1) {//up
        }
        if (keyCode == -2) {//down
        }
        if (keyCode == -3 || keyCode == 52) {//left
            statusArah = 3;
            if (status == 1) {
                server.broadcastToAll(String.valueOf(status), String.valueOf(statusArah));
            } else {
                client.broadcastToAll(String.valueOf(status), String.valueOf(statusArah));
            }
        }
        if (keyCode == -4 || keyCode == 54) {//right
            statusArah = 4;
            if (status == 1) {
                server.broadcastToAll(String.valueOf(status), String.valueOf(statusArah));
            } else {
                client.broadcastToAll(String.valueOf(status), String.valueOf(statusArah));
            }
        }
        if (keyCode == -7) {
            //System.out.println("yuk ah");
            midlet.exit();
            //midlet.showPlayGround(status);
        }
        if (keyCode == -6 || keyCode == 49) {
            String str = AyokLari.STATE_GAME.substring(0, 28);
            server.broadcastToClients(str + "1");
            AyokLari.STATE_GAME = str + "1";
        }
    }

    public void playerUpdate(Player arg0, String eventType, Object arg2) {
        if (eventType.equals(STARTED)) {
            delay = System.currentTimeMillis() - delay;
            repaint();
        } else if (eventType.equals(END_OF_MEDIA)) {
            delay = System.currentTimeMillis();
        }
    }

    private void playGameMusic() {
        try {
            sound01.stop();
            sound02.start();
        } catch (MediaException ex) {
        }
    }
}
